package com.ruoyi.classai.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.classai.domain.PxTraining;
import com.ruoyi.classai.domain.PxTrainingCourse;
import com.ruoyi.classai.domain.PxTrainingCourseComplete;
import com.ruoyi.classai.service.IPxTrainingCourseCompleteService;
import com.ruoyi.classai.service.IPxTrainingCourseService;
import com.ruoyi.classai.service.IPxTrainingService;
import com.ruoyi.common.core.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.classai.mapper.PxTrainingTeacherMapper;
import com.ruoyi.classai.domain.PxTrainingTeacher;
import com.ruoyi.classai.service.IPxTrainingTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 培训老师关联Service业务层处理
 * 
 * @author lcj
 * @date 2025-06-19
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PxTrainingTeacherServiceImpl extends ServiceImpl<PxTrainingTeacherMapper, PxTrainingTeacher> implements IPxTrainingTeacherService {


    @Autowired
    private IPxTrainingCourseCompleteService trainingCourseCompleteService;
    @Autowired
    private IPxTrainingCourseService trainingCourseService;
    @Autowired
    private IPxTrainingService trainingService;
    @Autowired
    private PxTrainingTeacherMapper trainingTeacherMapper;

    @Override
    public void addScore(String trainingId, String teacherId, Integer courseScore) {

        //todo 由于学分在培训计划里面，课程则不添加学分
        //查询关联课程信息
        PxTrainingTeacher trainingTeacher=this.getOne(Wrappers.<PxTrainingTeacher>lambdaQuery().eq(PxTrainingTeacher::getTrainingId,trainingId)
                .eq(PxTrainingTeacher::getTeacherId,teacherId));
        //添加分数
        //trainingTeacher.setTrainingScore(trainingTeacher.getTrainingScore()+courseScore);
        //todo 判断分数是否超过

        //计算进度
        List<PxTrainingCourse> courses=trainingCourseService.list(Wrappers.<PxTrainingCourse>lambdaQuery().eq(PxTrainingCourse::getTrainingId,trainingId)
                .eq(PxTrainingCourse::getStatus, Constants.COMMON_SUCCESS));
        //完成课程
        int completeNum=0;
        for (PxTrainingCourse course : courses) {
            PxTrainingCourseComplete courseComplete=trainingCourseCompleteService.getOne(Wrappers.<PxTrainingCourseComplete>lambdaQuery().eq(PxTrainingCourseComplete::getTeacherId,teacherId)
                    .eq(PxTrainingCourseComplete::getTrainingCourseId,course.getId()));
            //课程已完成
            if (Objects.nonNull(courseComplete)&&courseComplete.getStatus()==Constants.COMMON_SUCCESS){
                completeNum++;
            }
        }
        int result = (completeNum * 100) / courses.size();

        /*添加培训分数*/
        PxTraining pxTraining=trainingService.getById(trainingId);
        //判断学分发放形式
        if (0==pxTraining.getTrainingScoreType()){//市区发放
            log.info("市区发放积分");
        }else{ //平台发放
            log.info("平台发放积分");
            //判断是否全部完成
            if (result==100){
                trainingTeacher.setTrainingScore(pxTraining.getTrainingScore());
            }
        }
        //更新进度
        trainingTeacher.setTrainingProgress(result);
        this.updateById(trainingTeacher);
    }

    @Override
    public Integer getMyScore(String teacherId) {
        return trainingTeacherMapper.getMyScore(teacherId);
    }
}
